home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / vc / vc-hooks.el.z / vc-hooks.el
Encoding:
Text File  |  1998-05-21  |  46.9 KB  |  1,237 lines

  1. ;;; vc-hooks.el --- resident support for version-control
  2.  
  3. ;; Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
  4.  
  5. ;; Author:     Eric S. Raymond <esr@snark.thyrsus.com>
  6. ;; Maintainer: Andre Spiegel <spiegel@inf.fu-berlin.de>
  7. ;; Maintainer: (ClearCase) Rod Whitby <rwhitby@geocities.com>
  8. ;; XEmacs conversion: Steve Baur <steve@altair.xemacs.org>
  9.  
  10. ;; This file is part of GNU Emacs.
  11.  
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. ;; GNU General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  24. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  25. ;; Boston, MA 02111-1307, USA.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; This is the always-loaded portion of VC.
  30. ;; It takes care VC-related activities that are done when you visit a file,
  31. ;; so that vc.el itself is loaded only when you use a VC command.
  32. ;; See the commentary of vc.el.
  33.  
  34. ;; ClearCase support is being worked on in a parallel distribution of vc.
  35. ;; This file used to be dumped with XEmacs.  Autoload everything until we
  36. ;; get a clean method of specifying dumped elisp in packages.
  37.  
  38. ;;; Code:
  39.  
  40. ;; Customization Variables (the rest is in vc.el)
  41.  
  42. ;;;###autoload
  43. (defvar vc-default-back-end nil
  44.   "*Back-end actually used by this interface; may be SCCS or RCS.
  45. The value is only computed when needed to avoid an expensive search.")
  46.  
  47. ;;;###autoload
  48. (defvar vc-handle-cvs t
  49.   "*If non-nil, use VC for files managed with CVS.
  50. If it is nil, don't use VC for those files.")
  51.  
  52. ;;;###autoload
  53. (defvar vc-rcsdiff-knows-brief nil
  54.   "*Indicates whether rcsdiff understands the --brief option.
  55. The value is either `yes', `no', or nil.  If it is nil, VC tries
  56. to use --brief and sets this variable to remember whether it worked.")
  57.  
  58. ;;;###autoload
  59. (defvar vc-path
  60.   (if (file-directory-p "/usr/sccs")
  61.       '("/usr/sccs")
  62.     nil)
  63.   "*List of extra directories to search for version control commands.")
  64.  
  65. ;;;###autoload
  66. (defvar vc-master-templates
  67.   '(("%sRCS/%s,v" . RCS) ("%s%s,v" . RCS) ("%sRCS/%s" . RCS)
  68.     ("%sSCCS/s.%s" . SCCS) ("%ss.%s". SCCS)
  69.     vc-find-cvs-master)
  70.   "*Where to look for version-control master files.
  71. The first pair corresponding to a given back end is used as a template
  72. when creating new masters.")
  73.  
  74. ;;;###autoload
  75. (defvar vc-make-backup-files nil
  76.   "*If non-nil, backups of registered files are made as with other files.
  77. If nil (the default), files covered by version control don't get backups.")
  78.  
  79. ;;;###autoload
  80. (defvar vc-follow-symlinks 'ask
  81.   "*Indicates what to do if you visit a symbolic link to a file
  82. that is under version control.  Editing such a file through the
  83. link bypasses the version control system, which is dangerous and
  84. probably not what you want.  
  85.   If this variable is t, VC follows the link and visits the real file,
  86. telling you about it in the echo area.  If it is `ask', VC asks for
  87. confirmation whether it should follow the link.  If nil, the link is
  88. visited and a warning displayed.")
  89.  
  90. ;;;###autoload
  91. (defvar vc-display-status t
  92.   "*If non-nil, display revision number and lock status in modeline.
  93. Otherwise, not displayed.")
  94.  
  95. ;;;###autoload
  96. (defvar vc-consult-headers t
  97.   "*If non-nil, identify work files by searching for version headers.")
  98.  
  99. ;;;###autoload
  100. (defvar vc-keep-workfiles t
  101.   "*If non-nil, don't delete working files after registering changes.
  102. If the back-end is CVS, workfiles are always kept, regardless of the
  103. value of this flag.")
  104.  
  105. ;;;###autoload
  106. (defvar vc-mistrust-permissions nil
  107.   "*If non-nil, don't assume that permissions and ownership track 
  108. version-control status.  If nil, do rely on the permissions.
  109. See also variable `vc-consult-headers'.")
  110.  
  111. ;;;###autoload
  112. (defun vc-mistrust-permissions (file)
  113.   ;; Access function to the above.
  114.   (or (eq vc-mistrust-permissions 't)
  115.       (and vc-mistrust-permissions
  116.        (funcall vc-mistrust-permissions 
  117.             (vc-backend-subdirectory-name file)))))
  118.  
  119. ;; Tell Emacs about this new kind of minor mode
  120. ;;(if (not (assoc 'vc-mode minor-mode-alist))
  121. ;;    (setq minor-mode-alist (cons '(vc-mode vc-mode)
  122. ;;                 minor-mode-alist)))
  123.  
  124. ;; XEmacs:
  125. ;;;###autoload
  126. (add-minor-mode 'vc-mode 'vc-mode)
  127.  
  128. ;;;###autoload
  129. (defvar vc-mode nil)                    ; used for modeline flag
  130. ;; End XEmacs addition.
  131.  
  132. ;;;###autoload
  133. (make-variable-buffer-local 'vc-mode)
  134. ;;;###autoload
  135. (put 'vc-mode 'permanent-local t)
  136.  
  137. ;; We need a notion of per-file properties because the version
  138. ;; control state of a file is expensive to derive --- we compute
  139. ;; them when the file is initially found, keep them up to date 
  140. ;; during any subsequent VC operations, and forget them when
  141. ;; the buffer is killed.
  142.  
  143. ;;;###autoload
  144. (defmacro vc-error-occurred (&rest body)
  145.   (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
  146.  
  147. ;;;###autoload
  148. (defvar vc-file-prop-obarray [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  149.   "Obarray for per-file properties.")
  150.  
  151. ;;;###autoload
  152. (defvar vc-buffer-backend t)
  153. (make-variable-buffer-local 'vc-buffer-backend)
  154.  
  155. ;;;###autoload
  156. (defun vc-file-setprop (file property value)
  157.   ;; set per-file property
  158.   (put (intern file vc-file-prop-obarray) property value))
  159.  
  160. ;;;###autoload
  161. (defun vc-file-getprop (file property)
  162.   ;; get per-file property
  163.   (get (intern file vc-file-prop-obarray) property))
  164.  
  165. ;;;###autoload
  166. (defun vc-file-clearprops (file)
  167.   ;; clear all properties of a given file
  168.   (setplist (intern file vc-file-prop-obarray) nil))
  169.  
  170. ;;; Functions that determine property values, by examining the 
  171. ;;; working file, the master file, or log program output
  172.  
  173. ;;;###autoload
  174. (defun vc-match-substring (bn)
  175.   (buffer-substring (match-beginning bn) (match-end bn)))
  176.  
  177. ;;;###autoload
  178. (defun vc-lock-file (file)
  179.   ;; Generate lock file name corresponding to FILE
  180.   (let ((master (vc-name file)))
  181.     (and
  182.      master
  183.      (string-match "\\(.*/\\)s\\.\\(.*\\)" master)
  184.      (concat
  185.       (substring master (match-beginning 1) (match-end 1))
  186.       "p."
  187.       (substring master (match-beginning 2) (match-end 2))))))
  188.  
  189. ;;;###autoload
  190. (defun vc-parse-buffer (patterns &optional file properties)
  191.   ;; Use PATTERNS to parse information out of the current buffer.
  192.   ;; Each element of PATTERNS is a list of 2 to 3 elements. The first element
  193.   ;; is the pattern to be matched, and the second (an integer) is the 
  194.   ;; number of the subexpression that should be returned. If there's
  195.   ;; a third element (also the number of a subexpression), that 
  196.   ;; subexpression is assumed to be a date field and we want the most
  197.   ;; recent entry matching the template.
  198.   ;; If FILE and PROPERTIES are given, the latter must be a list of
  199.   ;; properties of the same length as PATTERNS; each property is assigned 
  200.   ;; the corresponding value.
  201.   (mapcar (function (lambda (p)
  202.         (goto-char (point-min))
  203.         (cond 
  204.          ((eq (length p) 2)  ;; search for first entry
  205.           (let ((value nil))
  206.         (if (re-search-forward (car p) nil t)
  207.             (setq value (vc-match-substring (elt p 1))))
  208.         (if file
  209.             (progn (vc-file-setprop file (car properties) value)
  210.                (setq properties (cdr properties))))
  211.         value))
  212.          ((eq (length p) 3)  ;; search for latest entry
  213.           (let ((latest-date "") (latest-val))
  214.         (while (re-search-forward (car p) nil t)
  215.           (let ((date (vc-match-substring (elt p 2))))
  216.             (if (string< latest-date date)
  217.             (progn
  218.               (setq latest-date date)
  219.               (setq latest-val
  220.                 (vc-match-substring (elt p 1)))))))
  221.         (if file
  222.             (progn (vc-file-setprop file (car properties) latest-val)
  223.                (setq properties (cdr properties))))
  224.         latest-val)))))
  225.       patterns)
  226.   )
  227.  
  228. ;;;###autoload
  229. (defun vc-insert-file (file &optional limit blocksize)
  230.   ;; Insert the contents of FILE into the current buffer.
  231.   ;; Optional argument LIMIT is a regexp. If present,
  232.   ;; the file is inserted in chunks of size BLOCKSIZE
  233.   ;; (default 8 kByte), until the first occurrence of
  234.   ;; LIMIT is found. The function returns nil if FILE 
  235.   ;; doesn't exist.
  236.   (erase-buffer)
  237.   (cond ((file-exists-p file)
  238.      (cond (limit
  239.         (if (not blocksize) (setq blocksize 8192))
  240.         (let (found s)
  241.           (while (not found)
  242.             (setq s (buffer-size))
  243.             (goto-char (1+ s))
  244.             (setq found 
  245.               (or (zerop (car (cdr 
  246.                   (insert-file-contents file nil s 
  247.                    (+ s blocksize)))))
  248.                   (progn (beginning-of-line)
  249.                      (re-search-forward limit nil t)))))))
  250.            (t (insert-file-contents file)))
  251.      (set-buffer-modified-p nil)
  252.      (auto-save-mode nil)
  253.      t)
  254.     (t nil)))
  255.  
  256. ;;;###autoload
  257. (defun vc-parse-locks (file locks)
  258.   ;; Parse RCS or SCCS locks.
  259.   ;; The result is a list of the form ((VERSION USER) (VERSION USER) ...),
  260.   ;; which is returned and stored into the property `vc-master-locks'.
  261.   (if (not locks) 
  262.       (vc-file-setprop file 'vc-master-locks 'none)
  263.     (let ((found t) (index 0) master-locks version user)
  264.       (cond ((eq (vc-backend file) 'SCCS)
  265.          (while (string-match "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
  266.                    locks index)
  267.            (setq version (substring locks 
  268.                     (match-beginning 1) (match-end 1)))
  269.            (setq user (substring locks 
  270.                      (match-beginning 2) (match-end 2)))
  271.            (setq master-locks (append master-locks 
  272.                       (list (cons version user))))
  273.            (setq index (match-end 0))))
  274.         ((eq (vc-backend file) 'RCS)
  275.          (while (string-match "[ \t\n]*\\([^:]+\\):\\([0-9.]+\\)"
  276.                   locks index)
  277.            (setq version (substring locks 
  278.                     (match-beginning 2) (match-end 2)))
  279.            (setq user (substring locks 
  280.                      (match-beginning 1) (match-end 1)))
  281.            (setq master-locks (append master-locks 
  282.                       (list (cons version user))))
  283.            (setq index (match-end 0)))
  284.          (if (string-match ";[ \t\n]+strict;" locks index)
  285.          (vc-file-setprop file 'vc-checkout-model 'manual)
  286.            (vc-file-setprop file 'vc-checkout-model 'implicit))))
  287.       (vc-file-setprop file 'vc-master-locks (or master-locks 'none)))))
  288.  
  289. ;;;###autoload
  290. (defun vc-simple-command (okstatus command file &rest args)
  291.   ;; Simple version of vc-do-command, for use in vc-hooks only.
  292.   ;; Don't switch to the *vc-info* buffer before running the
  293.   ;; command, because that would change its default directory
  294.   (save-excursion (set-buffer (get-buffer-create "*vc-info*"))
  295.           (erase-buffer))
  296.   (let ((exec-path (append vc-path exec-path)) exec-status
  297.     ;; Add vc-path to PATH for the execution of this command.
  298.     (process-environment
  299.      (cons (concat "PATH=" (getenv "PATH")
  300.                path-separator 
  301.                (mapconcat 'identity vc-path path-separator))
  302.            process-environment)))
  303.     (setq exec-status 
  304.       (apply 'call-process command nil "*vc-info*" nil 
  305.          (append args (list file))))
  306.     (cond ((> exec-status okstatus)
  307.        (switch-to-buffer (get-file-buffer file))
  308.        (shrink-window-if-larger-than-buffer
  309.         (display-buffer "*vc-info*"))
  310.        (error "Couldn't find version control information")))
  311.     exec-status))
  312.  
  313. ;;;###autoload
  314. (defun vc-fetch-master-properties (file)
  315.   ;; Fetch those properties of FILE that are stored in the master file.
  316.   ;; For an RCS file, we don't get vc-latest-version vc-your-latest-version
  317.   ;; here because that is slow.
  318.   ;; That gets done if/when the functions vc-latest-version
  319.   ;; and vc-your-latest-version get called.
  320.   (save-excursion
  321.     (cond
  322.      ((eq (vc-backend file) 'SCCS)
  323.       (set-buffer (get-buffer-create "*vc-info*"))
  324.       (if (vc-insert-file (vc-lock-file file))
  325.       (vc-parse-locks file (buffer-string))
  326.     (vc-file-setprop file 'vc-master-locks 'none))
  327.       (vc-insert-file (vc-name file) "^\001e")
  328.       (vc-parse-buffer 
  329.        (list '("^\001d D \\([^ ]+\\)" 1)
  330.          (list (concat "^\001d D \\([^ ]+\\) .* " 
  331.                (regexp-quote (vc-user-login-name)) " ") 1))
  332.        file
  333.        '(vc-latest-version vc-your-latest-version)))
  334.  
  335.      ((eq (vc-backend file) 'RCS)
  336.       (set-buffer (get-buffer-create "*vc-info*"))
  337.       (vc-insert-file (vc-name file) "^[0-9]")
  338.       (vc-parse-buffer 
  339.        (list '("^head[ \t\n]+\\([^;]+\\);" 1)
  340.          '("^branch[ \t\n]+\\([^;]+\\);" 1)
  341.          '("^locks[ \t\n]*\\([^;]*;\\([ \t\n]*strict;\\)?\\)" 1))
  342.        file
  343.        '(vc-head-version
  344.      vc-default-branch
  345.      vc-master-locks))
  346.       ;; determine vc-master-workfile-version: it is either the head
  347.       ;; of the trunk, the head of the default branch, or the 
  348.       ;; "default branch" itself, if that is a full revision number.
  349.       (let ((default-branch (vc-file-getprop file 'vc-default-branch)))
  350.     (cond 
  351.      ;; no default branch
  352.      ((or (not default-branch) (string= "" default-branch))
  353.       (vc-file-setprop file 'vc-master-workfile-version 
  354.                (vc-file-getprop file 'vc-head-version)))
  355.      ;; default branch is actually a revision
  356.      ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$" 
  357.             default-branch)
  358.       (vc-file-setprop file 'vc-master-workfile-version default-branch))
  359.      ;; else, search for the head of the default branch
  360.      (t (vc-insert-file (vc-name file) "^desc")
  361.         (vc-parse-buffer (list (list 
  362.            (concat "^\\(" 
  363.                (regexp-quote default-branch)
  364.                "\\.[0-9]+\\)\ndate[ \t]+\\([0-9.]+\\);") 1 2))
  365.              file '(vc-master-workfile-version)))))
  366.       ;; translate the locks
  367.       (vc-parse-locks file (vc-file-getprop file 'vc-master-locks)))
  368.  
  369.      ((eq (vc-backend file) 'CVS)
  370.       (save-excursion
  371.         ;; Call "cvs status" in the right directory, passing only the
  372.         ;; nondirectory part of the file name -- otherwise CVS might 
  373.         ;; silently give a wrong result.
  374.         (let ((default-directory (file-name-directory file)))
  375.           (vc-simple-command 0 "cvs" (file-name-nondirectory file) "status"))
  376.     (set-buffer (get-buffer "*vc-info*"))
  377.     (vc-parse-buffer     
  378.      ;; CVS 1.3 says "RCS Version:", other releases "RCS Revision:",
  379.      ;; and CVS 1.4a1 says "Repository revision:".
  380.      '(("\\(RCS Version\\|RCS Revision\\|Repository revision\\):[\t ]+\\([0-9.]+\\)" 2)
  381.        ("^File: [^ \t]+[ \t]+Status: \\(.*\\)" 1))
  382.      file
  383.      '(vc-latest-version vc-cvs-status))
  384.     ;; Translate those status values that we understand into symbols.
  385.     ;; Any other value is converted to nil.
  386.     (let ((status (vc-file-getprop file 'vc-cvs-status)))
  387.      (cond 
  388.       ((string-match "Up-to-date" status)
  389.        (vc-file-setprop file 'vc-cvs-status 'up-to-date)
  390.        (vc-file-setprop file 'vc-checkout-time 
  391.                 (nth 5 (file-attributes file))))
  392.       ((vc-file-setprop file 'vc-cvs-status
  393.         (cond 
  394.          ((string-match "Locally Modified"    status) 'locally-modified)
  395.          ((string-match "Needs Merge"         status) 'needs-merge)
  396.          ((string-match "Needs \\(Checkout\\|Patch\\)" status) 
  397.                                                           'needs-checkout)
  398.          ((string-match "Unresolved Conflict" status) 'unresolved-conflict)
  399.          ((string-match "Locally Added"       status) 'locally-added)
  400.          (t 'unknown)
  401.          ))))))))
  402.     (if (get-buffer "*vc-info*")
  403.     (kill-buffer (get-buffer "*vc-info*")))))
  404.  
  405. ;;; Functions that determine property values, by examining the 
  406. ;;; working file, the master file, or log program output
  407.  
  408. ;;;###autoload
  409. (defun vc-consult-rcs-headers (file)
  410.   ;; Search for RCS headers in FILE, and set properties
  411.   ;; accordingly.  This function can be disabled by setting
  412.   ;; vc-consult-headers to nil.  
  413.   ;; Returns: nil            if no headers were found 
  414.   ;;                         (or if the feature is disabled,
  415.   ;;                         or if there is currently no buffer 
  416.   ;;                         visiting FILE)
  417.   ;;          'rev           if a workfile revision was found
  418.   ;;          'rev-and-lock  if revision and lock info was found 
  419.   (cond
  420.    ((or (not vc-consult-headers) 
  421.     (not (get-file-buffer file))) nil)
  422.    ((let (status version locking-user)
  423.      (save-excursion
  424.       (set-buffer (get-file-buffer file))
  425.       (goto-char (point-min))
  426.       (cond  
  427.        ;; search for $Id or $Header
  428.        ;; -------------------------
  429.        ((or (and (search-forward "$Id: " nil t)
  430.          (looking-at "[^ ]+ \\([0-9.]+\\) "))
  431.         (and (progn (goto-char (point-min))
  432.             (search-forward "$Header: " nil t))
  433.          (looking-at "[^ ]+ \\([0-9.]+\\) ")))
  434.     (goto-char (match-end 0))
  435.     ;; if found, store the revision number ...
  436.     (setq version (buffer-substring-no-properties (match-beginning 1)
  437.                               (match-end 1)))
  438.     ;; ... and check for the locking state
  439.     (cond 
  440.      ((looking-at
  441.        (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] "             ; date
  442.         "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
  443.                "[^ ]+ [^ ]+ "))                       ; author & state
  444.       (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
  445.       (cond 
  446.        ;; unlocked revision
  447.        ((looking-at "\\$")
  448.         (setq locking-user 'none)
  449.         (setq status 'rev-and-lock))
  450.        ;; revision is locked by some user
  451.        ((looking-at "\\([^ ]+\\) \\$")
  452.         (setq locking-user
  453.           (buffer-substring-no-properties (match-beginning 1)
  454.                           (match-end 1)))
  455.         (setq status 'rev-and-lock))
  456.        ;; everything else: false
  457.        (nil)))
  458.      ;; unexpected information in
  459.      ;; keyword string --> quit
  460.      (nil)))
  461.        ;; search for $Revision
  462.        ;; --------------------
  463.        ((re-search-forward (concat "\\$" 
  464.                    "Revision: \\([0-9.]+\\) \\$")
  465.                nil t)
  466.     ;; if found, store the revision number ...
  467.     (setq version (buffer-substring-no-properties (match-beginning 1)
  468.                               (match-end 1)))
  469.     ;; and see if there's any lock information
  470.     (goto-char (point-min))
  471.     (if (re-search-forward (concat "\\$" "Locker:") nil t)
  472.         (cond ((looking-at " \\([^ ]+\\) \\$")
  473.            (setq locking-user (buffer-substring-no-properties
  474.                        (match-beginning 1)
  475.                        (match-end 1)))
  476.            (setq status 'rev-and-lock))
  477.           ((looking-at " *\\$") 
  478.            (setq locking-user 'none)
  479.            (setq status 'rev-and-lock))
  480.           (t 
  481.            (setq locking-user 'none)
  482.            (setq status 'rev-and-lock)))
  483.       (setq status 'rev)))
  484.        ;; else: nothing found
  485.        ;; -------------------
  486.        (t nil)))
  487.      (if status (vc-file-setprop file 'vc-workfile-version version))
  488.      (and (eq status 'rev-and-lock)
  489.       (eq (vc-backend file) 'RCS)
  490.       (vc-file-setprop file 'vc-locking-user locking-user)
  491.       ;; If the file has headers, we don't want to query the master file,
  492.       ;; because that would eliminate all the performance gain the headers
  493.       ;; brought us.  We therefore use a heuristic for the checkout model 
  494.       ;; now:  If we trust the file permissions, and the file is not 
  495.           ;; locked, then if the file is read-only the checkout model is 
  496.       ;; `manual', otherwise `implicit'.
  497.       (not (vc-mistrust-permissions file))
  498.       (not (vc-locking-user file))
  499.       (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
  500.           (vc-file-setprop file 'vc-checkout-model 'manual)
  501.         (vc-file-setprop file 'vc-checkout-model 'implicit)))
  502.      status))))
  503.  
  504. ;;; Access functions to file properties
  505. ;;; (Properties should be _set_ using vc-file-setprop, but
  506. ;;; _retrieved_ only through these functions, which decide
  507. ;;; if the property is already known or not. A property should
  508. ;;; only be retrieved by vc-file-getprop if there is no 
  509. ;;; access function.)
  510.  
  511. ;;; properties indicating the backend 
  512. ;;; being used for FILE
  513.  
  514. ;;;###autoload
  515. (defun vc-backend-subdirectory-name (&optional file)
  516.   ;; Where the master and lock files for the current directory are kept
  517.   (symbol-name
  518.    (or
  519.     (and file (vc-backend file))
  520.     vc-default-back-end
  521.     (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
  522.  
  523. ;;;###autoload
  524. (defun vc-name (file)
  525.   "Return the master name of a file, nil if it is not registered.
  526. For CVS, the full name of CVS/Entries is returned."
  527.   (or (vc-file-getprop file 'vc-name)
  528.       (let ((name-and-type (vc-registered file)))
  529.     (if name-and-type
  530.         (progn
  531.           (vc-file-setprop file 'vc-backend (cdr name-and-type))
  532.           (vc-file-setprop file 'vc-name (car name-and-type)))))))
  533.  
  534. ;;;###autoload
  535. (defun vc-backend (file)
  536.   "Return the version-control type of a file, nil if it is not registered."
  537.   (and file
  538.        (or (vc-file-getprop file 'vc-backend)
  539.        (let ((name-and-type (vc-registered file)))
  540.          (if name-and-type
  541.          (progn
  542.            (vc-file-setprop file 'vc-name (car name-and-type))
  543.            (vc-file-setprop file 'vc-backend (cdr name-and-type))))))))
  544.  
  545. ;;;###autoload
  546. (defun vc-checkout-model (file)
  547.   ;; Return `manual' if the user has to type C-x C-q to check out FILE.
  548.   ;; Return `implicit' if the file can be modified without locking it first.
  549.   (or
  550.    (vc-file-getprop file 'vc-checkout-model)
  551.    (cond 
  552.     ((eq (vc-backend file) 'SCCS)
  553.      (vc-file-setprop file 'vc-checkout-model 'manual))
  554.     ((eq (vc-backend file) 'RCS) 
  555.      (vc-consult-rcs-headers file)
  556.      (or (vc-file-getprop file 'vc-checkout-model)
  557.      (progn (vc-fetch-master-properties file)
  558.         (vc-file-getprop file 'vc-checkout-model))))
  559.     ((eq (vc-backend file) 'CVS)
  560.      (vc-file-setprop file 'vc-checkout-model
  561.               (if (getenv "CVSREAD") 'manual 'implicit))))))
  562.  
  563. ;;; properties indicating the locking state
  564.  
  565. ;;;###autoload
  566. (defun vc-cvs-status (file)
  567.   ;; Return the cvs status of FILE
  568.   ;; (Status field in output of "cvs status")
  569.   (cond ((vc-file-getprop file 'vc-cvs-status))
  570.     (t (vc-fetch-master-properties file)
  571.        (vc-file-getprop file 'vc-cvs-status))))
  572.  
  573. ;;;###autoload
  574. (defun vc-master-locks (file)
  575.   ;; Return the lock entries in the master of FILE.
  576.   ;; Return 'none if there are no such entries, and a list
  577.   ;; of the form ((VERSION USER) (VERSION USER) ...) otherwise.
  578.   (cond ((vc-file-getprop file 'vc-master-locks))
  579.     (t (vc-fetch-master-properties file)
  580.        (vc-file-getprop file 'vc-master-locks))))
  581.  
  582. ;;;###autoload
  583. (defun vc-master-locking-user (file)
  584.   ;; Return the master file's idea of who is locking 
  585.   ;; the current workfile version of FILE.  
  586.   ;; Return 'none if it is not locked.
  587.   (let ((master-locks (vc-master-locks file)) lock)
  588.     (if (eq master-locks 'none) 'none
  589.       ;; search for a lock on the current workfile version
  590.       (setq lock (assoc (vc-workfile-version file) master-locks))
  591.       (cond (lock (cdr lock))
  592.         ('none)))))
  593.  
  594. ;;;###autoload
  595. (defun vc-lock-from-permissions (file)
  596.   ;; If the permissions can be trusted for this file, determine the
  597.   ;; locking state from them.  Returns (user-login-name), `none', or nil.
  598.    ;;   This implementation assumes that any file which is under version
  599.   ;; control and has -rw-r--r-- is locked by its owner.  This is true
  600.   ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
  601.   ;; We have to be careful not to exclude files with execute bits on;
  602.   ;; scripts can be under version control too.  Also, we must ignore the
  603.   ;; group-read and other-read bits, since paranoid users turn them off.
  604.   ;;   This hack wins because calls to the somewhat expensive 
  605.   ;; `vc-fetch-master-properties' function only have to be made if 
  606.   ;; (a) the file is locked by someone other than the current user, 
  607.   ;; or (b) some untoward manipulation behind vc's back has changed 
  608.   ;; the owner or the `group' or `other' write bits.
  609.   (let ((attributes (file-attributes file)))
  610.     (if (not (vc-mistrust-permissions file))
  611.     (cond ((string-match ".r-..-..-." (nth 8 attributes))
  612.            (vc-file-setprop file 'vc-locking-user 'none))
  613.           ((and (= (nth 2 attributes) (user-uid))
  614.             (string-match ".rw..-..-." (nth 8 attributes)))
  615.            (vc-file-setprop file 'vc-locking-user (vc-user-login-name)))
  616.           (nil)))))
  617.  
  618. ;;;###autoload
  619. (defun vc-user-login-name (&optional uid)
  620.   ;; Return the name under which the user is logged in, as a string.
  621.   ;; (With optional argument UID, return the name of that user.)
  622.   ;; This function does the same as `user-login-name', but unlike
  623.   ;; that, it never returns nil.  If a UID cannot be resolved, that
  624.   ;; UID is returned as a string.
  625.   (or (user-login-name uid)
  626.       (and uid (number-to-string uid))
  627.       (number-to-string (user-uid))))
  628.  
  629. ;;;###autoload
  630. (defun vc-file-owner (file)
  631.   ;; Return who owns FILE (user name, as a string).
  632.   (vc-user-login-name (nth 2 (file-attributes file))))
  633.  
  634. ;;;###autoload
  635. (defun vc-rcs-lock-from-diff (file)
  636.   ;; Diff the file against the master version.  If differences are found,
  637.   ;; mark the file locked.  This is only used for RCS with non-strict
  638.   ;; locking.  (If "rcsdiff" doesn't understand --brief, we do a double-take
  639.   ;; and remember the fact for the future.)
  640.   (let* ((version (concat "-r" (vc-workfile-version file)))
  641.          (status (if (eq vc-rcsdiff-knows-brief 'no)
  642.                      (vc-simple-command 1 "rcsdiff" file version)
  643.                    (vc-simple-command 2 "rcsdiff" file "--brief" version))))
  644.     (if (eq status 2)
  645.         (if (not vc-rcsdiff-knows-brief)
  646.             (setq vc-rcsdiff-knows-brief 'no
  647.                   status (vc-simple-command 1 "rcsdiff" file version))
  648.           (error "rcsdiff failed."))
  649.       (if (not vc-rcsdiff-knows-brief) (setq vc-rcsdiff-knows-brief 'yes)))
  650.     (if (zerop status)
  651.         (vc-file-setprop file 'vc-locking-user 'none)
  652.       (vc-file-setprop file 'vc-locking-user (vc-file-owner file)))))
  653.  
  654. ;;;###autoload
  655. (defun vc-locking-user (file)
  656.   ;; Return the name of the person currently holding a lock on FILE.
  657.   ;; Return nil if there is no such person.
  658.   ;;   Under CVS, a file is considered locked if it has been modified since
  659.   ;; it was checked out.
  660.   ;;   The property is cached.  It is only looked up if it is currently nil.
  661.   ;; Note that, for a file that is not locked, the actual property value
  662.   ;; is `none', to distinguish it from an unknown locking state.  That value
  663.   ;; is converted to nil by this function, and returned to the caller.
  664.   (let ((locking-user (vc-file-getprop file 'vc-locking-user)))
  665.     (if locking-user
  666.     ;; if we already know the property, return it
  667.     (if (eq locking-user 'none) nil locking-user)
  668.  
  669.       ;; otherwise, infer the property...
  670.       (cond
  671.        ((eq (vc-backend file) 'CVS)
  672.     (or (and (eq (vc-checkout-model file) 'manual)
  673.          (vc-lock-from-permissions file))
  674.         (and (equal (vc-file-getprop file 'vc-checkout-time)
  675.             (nth 5 (file-attributes file)))
  676.          (vc-file-setprop file 'vc-locking-user 'none))
  677.         (vc-file-setprop file 'vc-locking-user (vc-file-owner file))))
  678.  
  679.        ((eq (vc-backend file) 'RCS)
  680.     (let (p-lock)
  681.  
  682.       ;; Check for RCS headers first
  683.       (or (eq (vc-consult-rcs-headers file) 'rev-and-lock)
  684.  
  685.           ;; If there are no headers, try to learn it 
  686.           ;; from the permissions.
  687.           (and (setq p-lock (vc-lock-from-permissions file))
  688.            (if (eq p-lock 'none)
  689.  
  690.                ;; If the permissions say "not locked", we know
  691.                ;; that the checkout model must be `manual'.
  692.                (vc-file-setprop file 'vc-checkout-model 'manual)
  693.  
  694.              ;; If the permissions say "locked", we can only trust
  695.              ;; this *if* the checkout model is `manual'.
  696.              (eq (vc-checkout-model file) 'manual)))
  697.  
  698.           ;; Otherwise, use lock information from the master file.
  699.           (vc-file-setprop file 'vc-locking-user
  700.                    (vc-master-locking-user file)))
  701.  
  702.       ;; Finally, if the file is not explicitly locked
  703.       ;; it might still be locked implicitly.
  704.       (and (eq (vc-file-getprop file 'vc-locking-user) 'none)
  705.            (eq (vc-checkout-model file) 'implicit)
  706.            (vc-rcs-lock-from-diff file))))
  707.  
  708.       ((eq (vc-backend file) 'SCCS)
  709.        (or (vc-lock-from-permissions file)
  710.        (vc-file-setprop file 'vc-locking-user 
  711.                 (vc-master-locking-user file)))))
  712.   
  713.       ;; convert a possible 'none value
  714.       (setq locking-user (vc-file-getprop file 'vc-locking-user))
  715.       (if (eq locking-user 'none) nil locking-user))))
  716.  
  717. ;;; properties to store current and recent version numbers
  718.  
  719. ;;;###autoload
  720. (defun vc-latest-version (file)
  721.   ;; Return version level of the latest version of FILE
  722.   (cond ((vc-file-getprop file 'vc-latest-version))
  723.     (t (vc-fetch-properties file)
  724.        (vc-file-getprop file 'vc-latest-version))))
  725.  
  726. ;;;###autoload
  727. (defun vc-your-latest-version (file)
  728.   ;; Return version level of the latest version of FILE checked in by you
  729.   (cond ((vc-file-getprop file 'vc-your-latest-version))
  730.     (t (vc-fetch-properties file)
  731.        (vc-file-getprop file 'vc-your-latest-version))))
  732.  
  733. ;;;###autoload
  734. (defun vc-master-workfile-version (file)
  735.   ;; Return the master file's idea of what is the current workfile version.
  736.   ;; This property is defined for RCS only.
  737.   (cond ((vc-file-getprop file 'vc-master-workfile-version))
  738.     (t (vc-fetch-master-properties file)
  739.        (vc-file-getprop file 'vc-master-workfile-version))))
  740.  
  741. ;;;###autoload
  742. (defun vc-fetch-properties (file)
  743.   ;; Fetch vc-latest-version and vc-your-latest-version
  744.   ;; if that wasn't already done.
  745.   (cond
  746.    ((eq (vc-backend file) 'RCS)
  747.     (save-excursion
  748.       (set-buffer (get-buffer-create "*vc-info*"))
  749.       (vc-insert-file (vc-name file) "^desc")
  750.       (vc-parse-buffer 
  751.        (list '("^\\([0-9]+\\.[0-9.]+\\)\ndate[ \t]+\\([0-9.]+\\);" 1 2)
  752.          (list (concat "^\\([0-9]+\\.[0-9.]+\\)\n"
  753.                "date[ \t]+\\([0-9.]+\\);[ \t]+"
  754.                "author[ \t]+"
  755.                (regexp-quote (vc-user-login-name)) ";") 1 2))
  756.        file
  757.        '(vc-latest-version vc-your-latest-version))
  758.       (if (get-buffer "*vc-info*")
  759.       (kill-buffer (get-buffer "*vc-info*")))))
  760.    (t (vc-fetch-master-properties file))
  761.    ))
  762.  
  763. ;;;###autoload
  764. (defun vc-workfile-version (file)
  765.   ;; Return version level of the current workfile FILE
  766.   ;; This is attempted by first looking at the RCS keywords.
  767.   ;; If there are no keywords in the working file, 
  768.   ;; vc-master-workfile-version is taken.
  769.   ;; Note that this property is cached, that is, it is only 
  770.   ;; looked up if it is nil.
  771.   ;; For SCCS, this property is equivalent to vc-latest-version.
  772.   (cond ((vc-file-getprop file 'vc-workfile-version))
  773.     ((eq (vc-backend file) 'SCCS) (vc-latest-version file))
  774.     ((eq (vc-backend file) 'RCS)
  775.      (if (vc-consult-rcs-headers file)
  776.          (vc-file-getprop file 'vc-workfile-version)
  777.        (let ((rev (cond ((vc-master-workfile-version file))
  778.                 ((vc-latest-version file)))))
  779.          (vc-file-setprop file 'vc-workfile-version rev)
  780.          rev)))
  781.     ((eq (vc-backend file) 'CVS)
  782.      (if (vc-consult-rcs-headers file)   ;; CVS
  783.          (vc-file-getprop file 'vc-workfile-version)
  784.        (catch 'found
  785.          (vc-find-cvs-master (file-name-directory file)
  786.                  (file-name-nondirectory file)))
  787.        (vc-file-getprop file 'vc-workfile-version)))))
  788.  
  789. ;;; actual version-control code starts here
  790.  
  791. ;;;###autoload
  792. (defun vc-registered (file)
  793.   (let (handler handlers)
  794.     (if (boundp 'file-name-handler-alist)
  795.     (setq handler (find-file-name-handler file 'vc-registered)))
  796.     (if handler
  797.     (funcall handler 'vc-registered file)
  798.       ;; Search for a master corresponding to the given file
  799.       (let ((dirname (or (file-name-directory file) ""))
  800.         (basename (file-name-nondirectory file)))
  801.     (catch 'found
  802.       (mapcar
  803.        (function (lambda (s)
  804.           (if (atom s)
  805.           (funcall s dirname basename)
  806.         (let ((trial (format (car s) dirname basename)))
  807.           (if (and (file-exists-p trial)
  808.                ;; Make sure the file we found with name
  809.                ;; TRIAL is not the source file itself.
  810.                ;; That can happen with RCS-style names
  811.                ;; if the file name is truncated
  812.                ;; (e.g. to 14 chars).  See if either
  813.                ;; directory or attributes differ.
  814.                (or (not (string= dirname
  815.                          (file-name-directory trial)))
  816.                    (not (equal
  817.                      (file-attributes file)
  818.                      (file-attributes trial)))))
  819.               (throw 'found (cons trial (cdr s))))))))
  820.        vc-master-templates)
  821.       nil)))))
  822.  
  823. ;;;###autoload
  824. (defun vc-find-cvs-master (dirname basename)
  825.   ;; Check if DIRNAME/BASENAME is handled by CVS.
  826.   ;; If it is, do a (throw 'found (cons MASTER 'CVS)).
  827.   ;; Note: This function throws the name of CVS/Entries
  828.   ;; NOT that of the RCS master file (because we wouldn't be able
  829.   ;; to access it under remote CVS).
  830.   ;; The function returns nil if DIRNAME/BASENAME is not handled by CVS.
  831.   (if (and vc-handle-cvs
  832.        (file-directory-p (concat dirname "CVS/"))
  833.        (file-readable-p (concat dirname "CVS/Entries")))
  834.       (let (buffer time (fold case-fold-search)
  835.         (file (concat dirname basename)))
  836.     (unwind-protect
  837.         (save-excursion
  838.           (setq buffer (set-buffer (get-buffer-create "*vc-info*")))
  839.           (vc-insert-file (concat dirname "CVS/Entries"))
  840.           (goto-char (point-min))
  841.           ;; make sure the file name is searched 
  842.           ;; case-sensitively
  843.           (setq case-fold-search nil)
  844.           (cond
  845.            ;; normal entry
  846.            ((re-search-forward
  847.          (concat "^/" (regexp-quote basename) 
  848.              "/\\([^/]*\\)/[^ /]* \\([A-Z][a-z][a-z]\\) *\\([0-9]*\\) \\([0-9]*\\):\\([0-9]*\\):\\([0-9]*\\) \\([0-9]*\\)")
  849.          nil t)
  850.         (setq case-fold-search fold)  ;; restore the old value
  851.         ;; We found it.  Store away version number now that we 
  852.         ;; are anyhow so close to finding it.
  853.         (vc-file-setprop file
  854.                  'vc-workfile-version
  855.                  (match-string 1))
  856.         ;; If the file hasn't been modified since checkout,
  857.         ;; store the checkout-time.
  858.         (let ((mtime (nth 5 (file-attributes file)))
  859.               (second (string-to-number (match-string 6)))
  860.               (minute (string-to-number (match-string 5)))
  861.               (hour (string-to-number (match-string 4)))
  862.               (day (string-to-number (match-string 3)))
  863.               (year (string-to-number (match-string 7))))
  864.           (if (equal mtime
  865.                  (encode-time
  866.                   second minute hour day
  867.                   (/ (string-match
  868.                   (match-string 2)
  869.                   "xxxJanFebMarAprMayJunJulAugSepOctNovDec")
  870.                  3)
  871.                   year 0))
  872.               (vc-file-setprop file 'vc-checkout-time mtime)
  873.             (vc-file-setprop file 'vc-checkout-time 0)))
  874.         (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
  875.            ;; entry for a "locally added" file (not yet committed)
  876.            ((re-search-forward
  877.          (concat "^/" (regexp-quote basename) "/0/Initial ") nil t)
  878.         (setq case-fold-search fold) ;; restore the old value
  879.         (vc-file-setprop file 'vc-checkout-time 0)
  880.         (vc-file-setprop file 'vc-workfile-version "0")
  881.         (throw 'found (cons (concat dirname "CVS/Entries") 'CVS)))
  882.            (t (setq case-fold-search fold)  ;; restore the old value
  883.           nil)))
  884.       (kill-buffer buffer)))))
  885.  
  886. ;;;###autoload
  887. (defun vc-buffer-backend ()
  888.   "Return the version-control type of the visited file, or nil if none."
  889.   (if (eq vc-buffer-backend t)
  890.       (setq vc-buffer-backend (vc-backend (buffer-file-name)))
  891.     vc-buffer-backend))
  892.  
  893. ;;;###autoload
  894. (defun vc-toggle-read-only (&optional verbose)
  895.   "Change read-only status of current buffer, perhaps via version control.
  896. If the buffer is visiting a file registered with version control,
  897. then check the file in or out.  Otherwise, just change the read-only flag
  898. of the buffer.  With prefix argument, ask for version number."
  899.   (interactive "P")
  900.   (if (vc-backend (buffer-file-name))
  901.       (vc-next-action verbose)
  902.     (toggle-read-only)))
  903. ;;;###autoload
  904. (define-key global-map "\C-x\C-q" 'vc-toggle-read-only)
  905.  
  906. ;;;###autoload
  907. (defun vc-after-save ()
  908.   ;; Function to be called by basic-save-buffer (in files.el).
  909.   ;; If the file in the current buffer is under version control,
  910.   ;; not locked, and the checkout model for it is `implicit',
  911.   ;; mark it "locked" and redisplay the mode line.
  912.   (let ((file (buffer-file-name)))
  913.     (and (vc-file-getprop file 'vc-backend)
  914.      ;; ...check the property directly, not through the function of the
  915.      ;; same name.  Otherwise Emacs would check for a master file
  916.      ;; each time a non-version-controlled buffer is saved.
  917.      ;; The property is computed when the file is visited, so if it
  918.      ;; is `nil' now, it is certain that the file is NOT 
  919.      ;; version-controlled.
  920.      (or (and (equal (vc-file-getprop file 'vc-checkout-time)
  921.              (nth 5 (file-attributes file)))
  922.           ;; File has been saved in the same second in which
  923.           ;; it was checked out.  Clear the checkout-time
  924.           ;; to avoid confusion.
  925.           (vc-file-setprop file 'vc-checkout-time nil))
  926.          t)
  927.      (not (vc-locking-user file))
  928.      (eq (vc-checkout-model file) 'implicit)
  929.      (vc-file-setprop file 'vc-locking-user (vc-user-login-name))
  930.      (or (and (eq (vc-backend file) 'CVS) 
  931.           (vc-file-setprop file 'vc-cvs-status nil))
  932.          t)
  933.      (vc-mode-line file))))
  934.  
  935. ;;;###autoload
  936. (defun vc-mode-line (file &optional label)
  937.   "Set `vc-mode' to display type of version control for FILE.
  938. The value is set in the current buffer, which should be the buffer
  939. visiting FILE.  Second optional arg LABEL is put in place of version
  940. control system name."
  941.   (interactive (list buffer-file-name nil))
  942.   (let ((vc-type (vc-backend file)))
  943.     (setq vc-mode
  944.       (and vc-type
  945.            (concat " " (or label (symbol-name vc-type)) 
  946.                (and vc-display-status (vc-status file)))))
  947.     ;; If the file is locked by some other user, make
  948.     ;; the buffer read-only.  Like this, even root
  949.     ;; cannot modify a file that someone else has locked.
  950.     (and vc-type 
  951.      (equal file (buffer-file-name))
  952.      (vc-locking-user file)
  953.      (not (string= (vc-user-login-name) (vc-locking-user file)))
  954.      (setq buffer-read-only t))
  955.     ;; If the user is root, and the file is not owner-writable,
  956.     ;; then pretend that we can't write it
  957.     ;; even though we can (because root can write anything).
  958.     ;; This way, even root cannot modify a file that isn't locked.
  959.     (and vc-type
  960.      (equal file (buffer-file-name))
  961.      (not buffer-read-only)
  962.      (zerop (user-real-uid))
  963.      (zerop (logand (file-modes (buffer-file-name)) 128))
  964.      (setq buffer-read-only t))
  965.     (force-mode-line-update)
  966.     ;;(set-buffer-modified-p (buffer-modified-p)) ;;use this if Emacs 18
  967.     vc-type))
  968.  
  969. ;;;###autoload
  970. (defun vc-status (file)
  971.   ;; Return string for placement in modeline by `vc-mode-line'.
  972.   ;; Format:
  973.   ;;
  974.   ;;   "-REV"        if the revision is not locked
  975.   ;;   ":REV"        if the revision is locked by the user
  976.   ;;   ":LOCKER:REV" if the revision is locked by somebody else
  977.   ;;   " @@"         for a CVS file that is added, but not yet committed
  978.   ;;
  979.   ;; In the CVS case, a "locked" working file is a 
  980.   ;; working file that is modified with respect to the master.
  981.   ;; The file is "locked" from the moment when the user saves
  982.   ;; the modified buffer.
  983.   ;; 
  984.   ;; This function assumes that the file is registered.
  985.  
  986.   (let ((locker (vc-locking-user file))
  987.     (rev (vc-workfile-version file)))
  988.     (cond ((string= "0" rev)
  989.        " @@")
  990.       ((not locker)
  991.        (concat "-" rev))
  992.       ((string= locker (vc-user-login-name))
  993.        (concat ":" rev))
  994.       (t 
  995.        (concat ":" locker ":" rev)))))
  996.  
  997. ;;;###autoload
  998. (defun vc-follow-link ()
  999.   ;; If the current buffer visits a symbolic link, this function makes it
  1000.   ;; visit the real file instead.  If the real file is already visited in 
  1001.   ;; another buffer, make that buffer current, and kill the buffer 
  1002.   ;; that visits the link.
  1003.   (let* ((truename (abbreviate-file-name (file-chase-links buffer-file-name)))
  1004.          (true-buffer (find-buffer-visiting truename))
  1005.      (this-buffer (current-buffer)))
  1006.     (if (eq true-buffer this-buffer)
  1007.     (progn
  1008.       (kill-buffer this-buffer)
  1009.       ;; In principle, we could do something like set-visited-file-name.
  1010.       ;; However, it can't be exactly the same as set-visited-file-name.
  1011.       ;; I'm not going to work out the details right now. -- rms.
  1012.       (set-buffer (find-file-noselect truename)))
  1013.       (set-buffer true-buffer)
  1014.       (kill-buffer this-buffer))))
  1015.  
  1016. ;;; install a call to the above as a find-file hook
  1017. ;;;###autoload
  1018. (defun vc-find-file-hook ()
  1019.   ;; Recompute whether file is version controlled,
  1020.   ;; if user has killed the buffer and revisited.
  1021.   (cond 
  1022.    (buffer-file-name
  1023.     (vc-file-clearprops buffer-file-name)
  1024.     (cond
  1025.      ((vc-backend buffer-file-name)
  1026.       (vc-mode-line buffer-file-name)
  1027.       (cond ((not vc-make-backup-files)
  1028.          ;; Use this variable, not make-backup-files,
  1029.          ;; because this is for things that depend on the file name.
  1030.          (make-local-variable 'backup-inhibited)
  1031.          (setq backup-inhibited t))))
  1032.      ((let* ((link (file-symlink-p buffer-file-name))
  1033.          (link-type (and link (vc-backend (file-chase-links link)))))
  1034.     (if link-type
  1035.             (cond ((eq vc-follow-symlinks nil)
  1036.                    (message
  1037.         "Warning: symbolic link to %s-controlled source file" link-type))
  1038.                   ((or (not (eq vc-follow-symlinks 'ask))
  1039.                ;; If we already visited this file by following
  1040.                ;; the link, don't ask again if we try to visit
  1041.                ;; it again.  GUD does that, and repeated questions
  1042.                ;; are painful.
  1043.                (get-file-buffer
  1044.             (abbreviate-file-name (file-chase-links buffer-file-name))))
  1045.                
  1046.            (vc-follow-link)
  1047.            (message "Followed link to %s" buffer-file-name)
  1048.            (vc-find-file-hook))
  1049.                   (t
  1050.                    (if (yes-or-no-p (format
  1051.         "Symbolic link to %s-controlled source file; follow link? " link-type))
  1052.                        (progn (vc-follow-link)
  1053.                               (message "Followed link to %s" buffer-file-name)
  1054.                               (vc-find-file-hook))
  1055.                      (message 
  1056.         "Warning: editing through the link bypasses version control")
  1057.                      ))))))))))
  1058.  
  1059. ;;;###autoload
  1060. (add-hook 'find-file-hooks 'vc-find-file-hook)
  1061.  
  1062. ;;; more hooks, this time for file-not-found
  1063. ;;;###autoload
  1064. (defun vc-file-not-found-hook ()
  1065.   "When file is not found, try to check it out from RCS or SCCS.
  1066. Returns t if checkout was successful, nil otherwise."
  1067.   (if (vc-backend buffer-file-name)
  1068.       (save-excursion
  1069.     (require 'vc)
  1070.     (setq default-directory (file-name-directory (buffer-file-name)))
  1071.     (not (vc-error-occurred (vc-checkout buffer-file-name))))))
  1072.  
  1073. ;;;###autoload
  1074. (add-hook 'find-file-not-found-hooks 'vc-file-not-found-hook)
  1075.  
  1076. ;; Discard info about a file when we kill its buffer.
  1077. ;;;###autoload
  1078. (defun vc-kill-buffer-hook ()
  1079.   (if (stringp (buffer-file-name))
  1080.       (progn
  1081.     (vc-file-clearprops (buffer-file-name))
  1082.     (kill-local-variable 'vc-buffer-backend))))
  1083.  
  1084. ;;;(add-hook 'kill-buffer-hook 'vc-kill-buffer-hook)
  1085.  
  1086. ;;; Now arrange for bindings and autoloading of the main package.
  1087. ;;; Bindings for this have to go in the global map, as we'll often
  1088. ;;; want to call them from random buffers.
  1089.  
  1090. ;;;###autoload
  1091. (setq vc-prefix-map (lookup-key global-map "\C-xv"))
  1092. ;;;###autoload
  1093. (if (not (keymapp vc-prefix-map))
  1094.     (progn
  1095.       (setq vc-prefix-map (make-sparse-keymap))
  1096.       (define-key global-map "\C-xv" vc-prefix-map)
  1097.       (define-key vc-prefix-map "a" 'vc-update-change-log)
  1098.       (define-key vc-prefix-map "c" 'vc-cancel-version)
  1099.       (define-key vc-prefix-map "d" 'vc-directory)
  1100.       (define-key vc-prefix-map "h" 'vc-insert-headers)
  1101.       (define-key vc-prefix-map "i" 'vc-register)
  1102.       (define-key vc-prefix-map "l" 'vc-print-log)
  1103.       (define-key vc-prefix-map "r" 'vc-retrieve-snapshot)
  1104.       (define-key vc-prefix-map "s" 'vc-create-snapshot)
  1105.       (define-key vc-prefix-map "u" 'vc-revert-buffer)
  1106.       (define-key vc-prefix-map "v" 'vc-next-action)
  1107.       (define-key vc-prefix-map "=" 'vc-diff)
  1108.       (define-key vc-prefix-map "~" 'vc-version-other-window)))
  1109.  
  1110. ;; Emacs menus
  1111. ;(if (not (boundp 'vc-menu-map))
  1112. ;    ;; Don't do the menu bindings if menu-bar.el wasn't loaded to defvar
  1113. ;    ;; vc-menu-map.
  1114. ;    ()
  1115. ;  ;;(define-key vc-menu-map [show-files]
  1116. ;  ;;  '("Show Files under VC" . (vc-directory t)))
  1117. ;  (define-key vc-menu-map [vc-directory] '("Show Locked Files" . vc-directory))
  1118. ;  (define-key vc-menu-map [separator1] '("----"))
  1119. ;  (define-key vc-menu-map [vc-rename-file] '("Rename File" . vc-rename-file))
  1120. ;  (define-key vc-menu-map [vc-version-other-window]
  1121. ;    '("Show Other Version" . vc-version-other-window))
  1122. ;  (define-key vc-menu-map [vc-diff] '("Compare with Last Version" . vc-diff))
  1123. ;  (define-key vc-menu-map [vc-update-change-log]
  1124. ;    '("Update ChangeLog" . vc-update-change-log))
  1125. ;  (define-key vc-menu-map [vc-print-log] '("Show History" . vc-print-log))
  1126. ;  (define-key vc-menu-map [separator2] '("----"))
  1127. ;  (define-key vc-menu-map [undo] '("Undo Last Check-In" . vc-cancel-version))
  1128. ;  (define-key vc-menu-map [vc-revert-buffer]
  1129. ;    '("Revert to Last Version" . vc-revert-buffer))
  1130. ;  (define-key vc-menu-map [vc-insert-header]
  1131. ;    '("Insert Header" . vc-insert-headers))
  1132. ;  (define-key vc-menu-map [vc-menu-check-in] '("Check In" . vc-next-action))
  1133. ;  (define-key vc-menu-map [vc-check-out] '("Check Out" . vc-toggle-read-only))
  1134. ;  (define-key vc-menu-map [vc-register] '("Register" . vc-register)))
  1135.  
  1136. ;(put 'vc-rename-file 'menu-enable 'vc-mode)
  1137. ;(put 'vc-version-other-window 'menu-enable 'vc-mode)
  1138. ;(put 'vc-diff 'menu-enable 'vc-mode)
  1139. ;(put 'vc-update-change-log 'menu-enable
  1140. ;     '(eq (vc-buffer-backend) 'RCS))
  1141. ;(put 'vc-print-log 'menu-enable 'vc-mode)
  1142. ;(put 'vc-cancel-version 'menu-enable 'vc-mode)
  1143. ;(put 'vc-revert-buffer 'menu-enable 'vc-mode)
  1144. ;(put 'vc-insert-headers 'menu-enable 'vc-mode)
  1145. ;(put 'vc-next-action 'menu-enable 'vc-mode)
  1146. ;(put 'vc-toggle-read-only 'menu-enable 'vc-mode)
  1147. ;(put 'vc-register 'menu-enable '(and buffer-file-name (not vc-mode)))
  1148.  
  1149. ;;;###autoload
  1150. (defconst vc-menu
  1151.   '("VC"
  1152.     :filter vc-menu-filter
  1153.     [""                    vc-next-action        buffer-file-name nil]
  1154.     ;;["Show Locked Files"       vc-directory t] ;; needs new dired
  1155.     "----"
  1156.     ["Revert to Last Revision"       vc-revert-buffer            vc-mode nil]
  1157.     ["Cancel Last Checkin"       vc-cancel-version        vc-mode]
  1158.     ["Rename File"           vc-rename-this-file        vc-mode nil]
  1159.     "----"
  1160.     ["Diff Against Last Version"   vc-diff            vc-mode]
  1161.     ["Diff Between Revisions..."   vc-version-diff        vc-mode]
  1162.     ;;["Ediff Between Revisions..."   ediff-revision        vc-mode]
  1163.     ["Visit Other Version..."       vc-version-other-window    vc-mode]
  1164.     ["Show Edit History"       vc-print-log            vc-mode]
  1165.     "----"
  1166.     ;; The two commented out List functions simply don't work at the
  1167.     ;; moment.
  1168.     ;;["List Locked Files"       (vc-directory '(16))        t]
  1169.     ["List Locked Files Any User"  vc-directory            t]
  1170.     ;;["List Registered Files"       (vc-directory '(4))        t]
  1171.     "----"
  1172.     ["Create Snapshot"               vc-create-snapshot         t]
  1173.     ["Retrieve Snapshot"       vc-retrieve-snapshot        t]
  1174.     "----"
  1175.     ["CVS Update Directory"          cvs-update                   t] ; pcl-cvs
  1176.     ;;["Show File Status"       vc-cvs-file-status        vc-mode]
  1177.     )
  1178.   "Menubar entry for using the revision control system.")
  1179.  
  1180. ;;;###autoload
  1181. (defun vc-menu-filter (menu-items)
  1182.   (let* ((result menu-items)        ; modify in-place
  1183.      (case-fold-search t)
  1184.      (type (vc-backend buffer-file-name))
  1185.      (file (if buffer-file-name
  1186.            (file-name-nondirectory buffer-file-name)
  1187.          (buffer-name)))
  1188.      op owner item status)
  1189.     (setq op (cond ((null type)
  1190.             "Register File")
  1191.            ((eq type 'CVS)
  1192.             (setq status
  1193.               (vc-file-getprop buffer-file-name 'cvs-status))
  1194.             (if status
  1195.             (cdr (assoc status
  1196.                     '(("Locally Modified" . "Commit")
  1197.                       ("Needs Merge" . "Merge with repository")
  1198.                       ("Up-to-date" . "Do nothing to")
  1199.                       ("Needs Checkout" . "Update"))))
  1200.               ;; #### - we're not gonna call cvs status just to
  1201.               ;; post a lousy menu...that's insane!
  1202.               "Next action on" 
  1203.               ))
  1204.            ;; these are all for RCS and SCCS
  1205.            ((not (setq owner (vc-file-owner file)))
  1206.             ;; #### - ugh!  this is broken.
  1207.             ;; vc-file-owner is not a suitable
  1208.             ;; substitute for vc-locking-user.
  1209.             "Check out File")
  1210.            ((not (string-equal owner (user-login-name)))
  1211.             "Steal File Lock")
  1212.            (t "Check in File")))
  1213.     (while (setq item (pop menu-items))
  1214.       (and (vectorp item)
  1215.        (cond ((eq 'vc-next-action (aref item 1))
  1216.           (aset item 0 op)
  1217.           (aset item 3 file))
  1218.          ((eq 'vc-file-status (aref item 1))
  1219.           (aset item 2 (eq 'CVS type))
  1220.           (aset item 3 file))
  1221.          ((> (length item) 3)
  1222.           (aset item 3 file)))))
  1223.     result))
  1224.  
  1225. ;;;###autoload
  1226. (and (featurep 'menubar)
  1227.      current-menubar
  1228.      (car (find-menu-item current-menubar '("Tools")))
  1229.      (add-submenu '("Tools") vc-menu "Compare")
  1230.      (add-menu-button '("Tools") "---" "Compare"))
  1231.  
  1232. ;;; End XEmacs menus
  1233.  
  1234. (provide 'vc-hooks)
  1235.  
  1236. ;;; vc-hooks.el ends here
  1237.